package org.springblade.modules.platform.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.modules.platform.dto.MeasuringChainInfoDTO;
import org.springblade.modules.platform.dto.MeasuringChainTableDTO;
import org.springblade.modules.platform.dto.MeasuringCompanyInfoDTO;
import org.springblade.modules.platform.entity.MeasuringChainInfo;
import org.springblade.modules.platform.entity.MeasuringChainTable;
import org.springblade.modules.platform.entity.MeasuringCompanyInfo;
import org.springblade.modules.platform.mapper.MeasuringChainInfoMapper;
import org.springblade.modules.platform.mapper.MeasuringChainTableMapper;
import org.springblade.modules.platform.mapper.MeasuringCompanyInfoMapper;
import org.springblade.modules.platform.mapper.MeasuringPlatformTypeMapper;
import org.springblade.modules.platform.service.IMeasuringChainTableService;
import org.springblade.modules.platform.vo.enums.DeptCategoryEnum;
import org.springblade.core.tool.utils.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SYG
 * @since 2023-05-22
 */
@Service
public class MeasuringChainTableServiceImpl extends ServiceImpl<MeasuringChainTableMapper, MeasuringChainTable> implements IMeasuringChainTableService {

    @Autowired
    MeasuringCompanyInfoMapper measuringCompanyInfoMapper;
    @Autowired
    MeasuringChainInfoMapper measuringChainInfoMapper;
    @Autowired
    MeasuringPlatformTypeMapper measuringPlatformTypeMapper;

    @Override
    public void insertMeasuringChainTable(MeasuringChainTableDTO measuringChainTableDTO) {


        switch (DeptCategoryEnum.getEnum(measuringChainTableDTO.getChainCategory())) {
            case DEPT:
                saveMeasuringChainTable(measuringChainTableDTO);
                break;
            case COMPANY:
                Long aLong = saveMeasuringChainTable(measuringChainTableDTO);
                MeasuringCompanyInfoDTO measuringCompanyInfoDTO = measuringChainTableDTO.getMeasuringCompanyInfoDTO();
                if(measuringCompanyInfoDTO!=null){
                    MeasuringCompanyInfo copy = BeanUtil.copy(measuringCompanyInfoDTO, MeasuringCompanyInfo.class);
                    copy.setMeasuringChainId(aLong);
                    measuringCompanyInfoMapper.insert(copy);
                }
                break;
            case MEASURING:
                MeasuringChainInfoDTO measuringChainInfoDTO = measuringChainTableDTO.getMeasuringChainInfoDTO();
                if(measuringChainInfoDTO!=null){
                    MeasuringChainInfo copy = BeanUtil.copy(measuringChainInfoDTO, MeasuringChainInfo.class);
                    copy.setMeasuringChainId(saveMeasuringChainTable(measuringChainTableDTO));
                    measuringChainInfoMapper.insert(copy);
                }
                break;

        }
    }

    @Override
    public void removeMeasuringChain(Long id) {
        MeasuringChainTable measuringChainTable = baseMapper.selectById(id);
        measuringChainTable.setIsDeleted(1);
        measuringChainTable.setUpdateTime(System.currentTimeMillis());
        baseMapper.updateById(measuringChainTable);
    }

    @Override
    public void upateMeasuringChainTable(MeasuringChainTableDTO measuringChainTableDTO) {
        switch (DeptCategoryEnum.getEnum(measuringChainTableDTO.getChainCategory())) {
            case DEPT:
                updateMeasuringChainTable(measuringChainTableDTO);
                break;
            case COMPANY:
                measuringCompanyInfoMapper.delete(Wrappers.<MeasuringCompanyInfo>query().lambda().eq(MeasuringCompanyInfo::getMeasuringChainId,measuringChainTableDTO.getId()));
                updateMeasuringChainTable(measuringChainTableDTO);
                MeasuringCompanyInfoDTO measuringCompanyInfoDTO = measuringChainTableDTO.getMeasuringCompanyInfoDTO();
                if(measuringCompanyInfoDTO!=null){
                    MeasuringCompanyInfo copy = BeanUtil.copy(measuringCompanyInfoDTO, MeasuringCompanyInfo.class);
                    copy.setMeasuringChainId(measuringChainTableDTO.getId());
                    measuringCompanyInfoMapper.insert(copy);
                }
                break;
            case MEASURING:
                measuringCompanyInfoMapper.delete(Wrappers.<MeasuringCompanyInfo>query().lambda().eq(MeasuringCompanyInfo::getMeasuringChainId,measuringChainTableDTO.getId()));
                updateMeasuringChainTable(measuringChainTableDTO);
                MeasuringChainInfoDTO measuringChainInfoDTO = measuringChainTableDTO.getMeasuringChainInfoDTO();
                if(measuringChainInfoDTO!=null){
                    MeasuringChainInfo copy = BeanUtil.copy(measuringChainInfoDTO, MeasuringChainInfo.class);
                    copy.setMeasuringChainId(measuringChainTableDTO.getId());
                    measuringChainInfoMapper.updateById(copy);
                }
                break;
        }
    }

    private Long saveMeasuringChainTable(MeasuringChainTableDTO measuringChainTableDTO){
        MeasuringChainTable measuringChainTable = BeanUtil.copy(measuringChainTableDTO, MeasuringChainTable.class);
        MeasuringChainTable measuringChainTable1 = baseMapper.selectById(measuringChainTable.getParentId());
        if(measuringChainTable1==null){
            measuringChainTable.setAncestors("0");
        }else {
            measuringChainTable.setAncestors(measuringChainTable1.getAncestors()+","+measuringChainTable1.getId());
        }
        measuringChainTable.setInsertTime(System.currentTimeMillis());
        measuringChainTable.setUpdateTime(System.currentTimeMillis());
        measuringChainTable.setIsDeleted(0);
        baseMapper.insert(measuringChainTable);
        return measuringChainTable.getId();
    }

    private void updateMeasuringChainTable(MeasuringChainTableDTO measuringChainTableDTO){
        MeasuringChainTable measuringChainTable = BeanUtil.copy(measuringChainTableDTO, MeasuringChainTable.class);
        baseMapper.updateById(measuringChainTable);
    }
}
